package gui;

import gui.AbstractViewPane.ViewMode;
import gui.nodes.AbstractNode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;

import business.UEBL;

public class UEUIConsulter extends AbstractViewPane {

	private ViewMode currentViewMode;
	
	//champs du formulaire
	private JTextField textFieldNom;
	private JSpinner spinECTS;
	private JSpinner spinNote;
	private JTextArea textAreaDesc;
    
	//variables d'affichage du formulaire
	private int id = -1;
	private int semestreId;
	private String nom = ""; 
	private int ects = 0; 
	private int note = 0; 
	private String desc = ""; 
	private AbstractNode creator;
	
	private ComponentList componentList = new ComponentList();
	
	public UEUIConsulter(AbstractNode creator, AbstractViewPane.ViewMode v, int ueId, int semId){
		this.creator = creator;
		this.currentViewMode = v;
		id = ueId;
		semestreId = semId;
		initGUI(ueId);
	}
	
	@Override
	public String getTitle() {
		System.out.println("consultation : " + currentViewMode);
		if (currentViewMode == ViewMode.CONSULTATION){
			return "Consultation d'une UE";
		}else if (currentViewMode == ViewMode.AJOUT){
			return "Ajout d'une nouvelle UE";
		}else if (currentViewMode == ViewMode.MODIFICATION){
			return "Modification d'une UE";
		}else {
			return "probleme avec le viewmode";
		}
	}
	
	
	@Override
	public boolean hasBeenModified() {
		return  (currentViewMode != ViewMode.CONSULTATION);
	}
	
	
	private void initGUI(int ueId) {

		UEBL ueBL;
		
		//si l'id du noeud envoye est -1, on est en mode ajout, les champs seront donc vides
		if (currentViewMode != ViewMode.AJOUT){
        	//si on est en consultation ou modification (recuperation des elements en base)
        	ueBL = new UEBL(ueId);
        	id = ueId;
		    nom = ueBL.getNomUE();
		    ects = ueBL.getNbECTS();
		    note = ueBL.getNoteMinUE();
		    desc = ueBL.getDescriptionUE();
        }
		
	    //Create and populate the panel.
	    JPanel p = new JPanel(new SpringLayout());
	        
	    JLabel lnom = new JLabel("Nom de l'UE : ", JLabel.TRAILING);
	    p.add(lnom);
		lnom.setLabelFor(textFieldNom);
		
		textFieldNom = new JTextField(nom);
	    p.add(textFieldNom);
	    componentList.add(textFieldNom);
		
	    JLabel lects = new JLabel("Nombre d'ECTS : ", JLabel.TRAILING);
	    p.add(lects);
		lects.setLabelFor(spinECTS);
		
		SpinnerModel sm = new SpinnerNumberModel(ects, 0, 50, 1);
        spinECTS = new JSpinner(sm);
        p.add(spinECTS);
        componentList.add(spinECTS);
		
	    JLabel lnote = new JLabel("Note minimale : ", JLabel.TRAILING);
	    p.add(lnote);
		lnote.setLabelFor(spinNote);
		
		sm = new SpinnerNumberModel(note, 0, 20, 1);
        spinNote = new JSpinner(sm);
        p.add(spinNote);
        componentList.add(spinNote);
		
	    JLabel ldesc = new JLabel("Description : ", JLabel.TRAILING);
	    p.add(ldesc);
		ldesc.setLabelFor(textAreaDesc);

        textAreaDesc = new JTextArea(desc);
        p.add(textAreaDesc);
		textAreaDesc.setRows(5);   
		componentList.add(textAreaDesc);

        	
		//Lay out the panel.
		SpringUtilities.makeCompactGrid(p,
	                                    4, 2, //rows, cols   le +2 pour les 2 combobox
	                                    6, 6,        //initX, initY
	                                    6, 6);       //xPad, yPad
		add(p, BorderLayout.NORTH);
	        
		
		if (currentViewMode != ViewMode.CONSULTATION) {
			// Mode modification - afficher les boutons		    
			JPanel commandBox = new JPanel();
			JButton save = new JButton("Enregistrer");
			save.setActionCommand("Enregistrer");
			save.addActionListener(new UEListener());
			commandBox.add(save);
			drawCommandBox(commandBox);
	    }
		else {
			componentList.disableAll();
		}
	}
	
	//inner class chargee de gerer l'ecoute du bouton d'enregistrement du formulaire
	private class UEListener implements ActionListener {
		
		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getActionCommand().equals("Enregistrer")){
				System.out.println("Demande d'enregistrement");
				UEBL ueSave = new UEBL();
				//si -1, c'est un ajout d'enseignant, sinon modification de l'enseignant de l'id concerne
				if(getId() == -1){
					ueSave.ajouterUE(getId(), getNom(), getDesc(), getECTS(),
							getNote(), getIdSemestre(),null);
					componentList.disableAll();
				} else {
					ueSave = new UEBL(id);
					ueSave.modifierUE(getId(), getNom(), getDesc(), getECTS(),
							getNote(), getIdSemestre(),null);
					componentList.disableAll();
				}
				creator.refresh();
			}	
		}
	}
	
	//methodes de recupperation des elements des champs
	public int getId(){
		return id;
	}
	public String getNom(){
		return textFieldNom.getText();
	}
	public int getECTS(){
		return Integer.parseInt(spinECTS.getValue().toString());
	}
	public int getNote(){
		return Integer.parseInt(spinNote.getValue().toString());
	}
	public String getDesc(){
		return textAreaDesc.getText();
	}
	public int getIdSemestre(){
		return semestreId;
	}
}